﻿//上传
function returnImgStr(obj) {
    let flag = new Array();
    
    var arr = obj[0].files;
     var len=arr.length;
    var index = layer.load(0, {
                shade: [0.2, '#000'] //0.1透明度的白色背景
            });
    for (let i = 0; i < len; i++) {
      
        var form = new FormData();
         form.append('file', arr[i]);
        $.ajax({
                type: 'POST',
                async: false,
                processData: false,
                contentType: false,
                url: preUrl + "/m/user/ajaxUploadImage",
                data: form
            })
            .done(function(res) {
                if (res.suc) {
                    flag.push(res.result);
                } else {

                }
            })
    }
    layer.close(index);
     return flag.join(",");
  
}

//查询数据
function getData(url) {
    let flag = new Array();
    $.ajax({
            type: 'POST',
            async: false,
            url: preUrl + url,
            data: {
                pageNo: 1,
                PageSize: 1000
            }
        })
        .done(function(res) {
            if (res.suc) {
                var keyStr = window.location.href.split("home/")[1].split(".html")[0];
                if (keyStr.indexOf('zExpress') > -1 || keyStr.indexOf('goods') > -1) {
                    flag = res.result;
                } else if (url.indexOf('Logistics') > -1||url.indexOf('gettemSetup') > -1) {

                    flag = res.result;

                } else {

                    flag = res.result.content;

                }

            } else {
                flag = false;
            }
        })
    return flag;
}



function checkPage(i, obj) {
    for (let j in obj) {
        if (i == j) {
            return obj[j];
        }
    }
}


//返回当前页字典对象
function checkCurPage() {
    var keyStr = window.location.href.split("home/")[1].split(".html")[0];
    switch (keyStr) {
        case "zExpress":
            return zExpressKey;
            break;
        case "goods":
            return goodsKey;
            break;
        case "admin":
            return adminKey;
            break;
        case "replace":
            return replaceKey;
            break;
        case "unCheck":
            return unCheckKey;
            break;
        case "allOrder":
            return allOrderKey;
            break;
        case "orderReq":
            return allOrderKey;
            break;
        case "report":
            return reportKey;
            break;
        case "good":
            return goodKey;
            break;
        case "user":
            return userKey;
            break;
        case "unuser":
            return userKey;
            break;
             case "setting":
            return goodKey;
            break;
        default:
            break;

    };

}


//翻译当前页字段含义
function translateName(i) {
    var keyStr = window.location.href.split("home/")[1].split(".html")[0];
    switch (keyStr) {
        case "zExpress":
            return checkPage(i, zExpressKey);
            break;
        case "goods":
            return checkPage(i, goodsKey);
            break;
        case "admin":
            return checkPage(i, adminKey);
            break;
        case "replace":
            return checkPage(i, replaceKey);
            break;
        case "unCheck":
            return checkPage(i, unCheckKey);
            break;
        case "allOrder":
            return checkPage(i, allOrderKey);
            break;
        case "orderReq":
            return checkPage(i, allOrderKey);
            break;
        case "report":
            return checkPage(i, reportKey);
            break;
        case "good":
            return checkPage(i, goodKey);
            break;
        case "user":
            return checkPage(i, userKey);
            break;
        case "unuser":
            return checkPage(i, userKey);
            break;
        case "setting":
            return checkPage(i, goodKey);
            break;
        default:
            break;
    };

}

function returnFormText(fName, type) {
    switch (type) {
        case "text":
            return "<div class='layui-form-item'><label class='layui-form-label'>" + translateName(fName) + "</label><div class='layui-input-block'><input type='text' name='" + fName + "'  lay-verify='required' autocomplete='off' placeholder='请输入' class='layui-input'></div></div>";
            break;
        case "radio":
            return "<div class='layui-form-item'><label class='layui-form-label'>" + translateName(fName) + "</label><div class='layui-input-block'><input type='radio' name='" + fName + "'  value='1' title='是'  ><input type='radio' name='" + fName + "' value='0' title='否' checked=''> </div></div>";
            break;
        case "file":
            return "<div class='layui-form-item'><label class='layui-form-label'>" + translateName(fName) + "</label><div class='layui-input-block'><div class='layui-btn layui-btn-normal'>上传" + translateName(fName) + "</div><input multiple type='file' name='" + fName + "' class='uploadBtn' /></div></div>";
            break;
        default:
            break;
    }
}



function returnFormType(fName) {

    if (TextKey.indexOf(fName) > -1) {
        return returnFormText(fName, "text");
    } else if (CheckboxKey.indexOf(fName) > -1) {
        return returnFormText(fName, "radio");
    } else if (FileKey.indexOf(fName) > -1) {
        return returnFormText(fName, "file");
    }
}

function add(url) {
    $(".addForm").html("");

    for (let i in checkCurPage()) {
        console.log(i)
        $(".addForm").append($(returnFormType(i)));
    }

    $(".addForm").css("padding-top", "30px");
    $(".addForm").css("background-color", "#f2f2f2");
    var submit = $("<div class='layui-form-item'><label class='layui-form-label'></label><div class='layui-input-block'><button class='layui-btn' lay-submit='' lay-filter='formDemo'>立即提交</button>&nbsp;&nbsp;<button type='reset' class='layui-btn layui-btn-primary cancelAdd'>取消</button></div></div>");
    $(".addForm").append(submit);
    $(".addForm").show();


    layui.use(['table', 'form', 'upload'], function() {
        var form = layui.form;
        var upload = layui.upload;
        var table = layui.table;

        //监听提交
        form.on('submit(formDemo)', function(data) {
            var index = layer.load(0, {
                shade: [0.2, '#000'] //0.1透明度的白色背景
            });

            $(".addForm").hide();
            var formData = new FormData();
            if (url.indexOf("addExpress") > -1 || url.indexOf("getGoods") > -1) {
                formData.append("adminid", getSession('if').id);
            }
            for (let j in data.field) {
                formData.append(j, data.field[j]);
            }
            $.ajax({
                    type: 'POST',
                    async: false,
                    url: preUrl + url,
                    contentType: false,
                    processData: false,
                    data: formData
                })
                .done(function(res) {
                    layer.msg(res.msg);
                    if (res.code == 100) {



                    } else {


                    }
                })

        });


    })



}

//详情
function detail(data) {
    $(".addForm").html("");
    for (let i in data) {
        var item = $("<div style='line-height:45px;padding-left:200px;'>" + translateName(i) + ":" + data[i] + "</div>");
        $(".addForm").append(item);
    }
    var button = $("<button style='line-height:45px;margin:80px 200px;'  class='clear layui-btn layui-btn-md'>关闭</button>");
    $(".addForm").prepend(button);
    $(".addForm").css("background-color", "#F2F2F2");
    $(".addForm").show();

    layui.use(['table', 'form', 'upload'], function() {
        var form = layui.form;
        var upload = layui.upload;
        var table = layui.table;
        var table = layui.table
        form.render();

    })

}
//审核
function checkUser(url, formdata) {
    let index = layer.load(0, { shade: [0.3, "#000"] });
    $.ajax({
            type: 'POST',
            url: preUrl + url,
            processData: false,
            contentType: false,
            data: formdata
        })
        .done(function(res) {
            layer.close(index);
            layer.msg(res.msg)
            if (res.code == 100) {


            } else {


            }
        })
}


//删除 
function delData(url, formdata, obj) {
    let index = layer.load(0, { shade: [0.3, "#000"] });
    $.ajax({
            type: 'POST',
            url: preUrl + url,
            processData: false,
            contentType: false,
            data: formdata
        })
        .done(function(res) {
            layer.close(index);
            if (res.code == 100) {
                layer.msg("删除成功")
                obj.del();
            } else {


            }
        })
}

function changeData(url, data) {
    let index = layer.load(0, { shade: [0.3, "#000"] });

    var settings = {
        "url": preUrl + url,
        "method": "POST",
        "processData": false,
        "contentType": false,
        "data": data
    }

    $.ajax(settings).done(function(res) {
        layer.close(index);

        layer.msg(res.msg);

    });

}

$(".addForm").on("click", ".clear", function() {
    $(this).parent().html("");

    $(this).parent().hide();
})

$(".addForm").on("click", ".cancelAdd", function() {
    $(".addForm").html("");
    $(".addForm").hide();
})

//追加JSOn表格到对象obj
function initJsonArray(arr, obj) {

    let newArr = new Array();
    let preObj = {
        fixed: 'left',
        type: 'checkbox'
    };
    let laterObj = {
        fixed: 'right',
        title: '操作',
        toolbar: '#barDemo2'
    };
    newArr.push(preObj);
    for (let i in arr[0]) {
        let objItem = new Object();
        objItem.field = i;
        objItem.title = translateName(i);
        objItem.sort = true;
        objItem.edit = "text";
        if (i.toLowerCase().indexOf('id') > -1) {

            objItem.title = i;
            delete objItem.edit;

        }
        if (i.indexOf('typeCode') > -1) {

            objItem.title = i;
            delete objItem.edit;

        }
        newArr.push(objItem);
    }
    newArr.push(laterObj);
    var finalArr = new Array();
    finalArr.push(newArr);
    layui.use(['table', 'form'], function() {
        var form = layui.form;
        var table = layui.table;
        layui.table.render({
            elem: obj,
            cellMinWidth: 170,
            title: '数据表',
            page: true,
            limit: 10,
            cols: finalArr,
            data: arr

        });
        table.on('tool()', function(obj) {


        });
    })


}





function initTable(url) {



    var  data = getData(url);




    //  if (!data) {
    //     return false;
    // }
    let newArr = new Array();
    let preObj = {
        fixed: 'left',
        type: 'checkbox'
    };
    let laterObj = {
        fixed: 'right',
        title: '操作',
        toolbar: '#barDemo'
    };


    newArr.push(preObj);
   
    if(data.zd){
        var data1=new Array();
        data1.push(data);
        var data=data1;
    }
    for (let i in data[0]) {
        let objItem = new Object();
        objItem.field = i;

        objItem.title = translateName(i);
        objItem.sort = true;
        objItem.edit = "text";

        if (i.toLowerCase().indexOf('id') > -1) {
            objItem.title = i;
            delete objItem.edit;

        }

        newArr.push(objItem);
    }
    newArr.push(laterObj);

    var finalArr = new Array();
    finalArr.push(newArr);

    layui.use(['table', 'form'], function() {


        var form = layui.form;
        var table = layui.table;
        layui.table.render({
            elem: '#test',
            toolbar: '#mytoolbar',
            cellMinWidth: 170,
            // url: preUrl + url,
            //  method: "POST",
            title: '数据表',
            cols: finalArr,
            data: data,
            page: true,
            // request: {
            //     pageName: 'pageNo',
            //     limitName: 'PageSize'
            // },
            // where: {
            //     pageNo:1,
            //     PageSize:1000,
            // },
            // page: true,
            // response:{
            //     statusName:'suc',
            //     statusCode:true,
            //     msgName:'msg',
            //     countName:'totalElements',
            //     dataName: 'result'
            // },

            // parseData: function(res) {

            //     return {
            //         "code": res.suc, 
            //          "msg": res.msg,  
            //          "count": res.result.content.length,  
            //         "data": res.result.content  
            //     }

            // }



        });
        //监听单元格编辑
        table.on('edit', function(obj) {

            var value = obj.value,
                data = obj.data,
                field = obj.field; //得到字段
            var formdata = new FormData();
            if (url.indexOf("Express") > -1) {
                for (let j in obj.data) {
                    formdata.append(j, obj.data[j]);
                }
                formdata.append("expressid", obj.data.id);
                changeData("/a/sys/updateExpress", formdata);
            } else if (url.indexOf("getGoodCat") > -1) {
                for (let j in obj.data) {
                    formdata.append(j, obj.data[j]);
                }
                formdata.append("goodcatid", obj.data.catId);
                formdata.append("adminid", getSession('if').id);
                changeData("/a/sys/updateGoodCat", formdata);
            }else if (url.indexOf("gettemSetup") > -1) {
                for (let j in obj.data) {
                    formdata.append(j, obj.data[j]);
                }
              
                formdata.append("adminid", getSession('if').id);
                changeData("/a/sys/systemSetup", formdata);
            }

        });
        table.on('addData', function(obj) {
            var value = obj.value,
                data = obj.data,
                field = obj.field; //得到字段

        });

        //监听行工具事件
        table.on('tool()', function(obj) {
            var data = obj.data;
            var form = new FormData();
            var adminid = getSession("if").id;
            if (obj.event == 'del') {
                layer.confirm('真的删除么', function(index) {
                    layer.close(index);
                    if (url.indexOf("Express") > -1) {
                        form.append("adminid", adminid);
                        form.append("expressid", obj.data.id);
                        delData("/a/sys/deleteExpress", form, obj);
                        obj.del();
                    } else if (url.indexOf("GoodCat") > -1) {

                        form.append("adminid", adminid);
                        form.append("goodcatid", obj.data.catId);
                        delData("/a/sys/deleteGoodCat", form, obj);
                        obj.del();
                    } else if (url.indexOf("getGoods") > -1) {

                        form.append("adminid", adminid);
                        form.append("goodId", obj.data.goodid);
                        delData("/a/sys/deleteGoods", form, obj);
                        obj.del();
                    }



                });
            } else if (obj.event == 'edit') {
                layer.msg("单击单元格修改..");
            } else if (obj.event == 'detail') {
                detail(obj.data);
            } else if (obj.event == "pass") {
                var form = new FormData();
                form.append('applyid', obj.data.id);
                form.append('adminid', adminid);
                checkUser("/a/sys/adopt", form);
            } else if (obj.event == "refuse") {
                var form = new FormData();
                form.append('applyid', obj.data.id);
                form.append('adminid', adminid);
                layer.prompt({ title: '输入拒绝理由', formType: 2 }, function(pass, index) {
                    layer.close(index);
                    form.append('count', pass);
                    checkUser("/a/sys/refuse", form);
                });
            } else if (obj.event == 'upGoods') {
                var form = new FormData();
                form.append('goodId', obj.data.goodid);
                form.append('adminid', adminid);
                checkUser("/a/sys/onShelfGoods", form);
            } else if (obj.event == 'downGoods') {
                var form = new FormData();
                form.append('goodId', obj.data.goodid);
                form.append('adminid', adminid);
                checkUser("/a/sys/lowerframeGoods", form);
            } else if (obj.event == 'freeze') {
                var form = new FormData();
                form.append('userid', obj.data.id);
                form.append('adminid', adminid);
                checkUser("/a/sys/Frozen", form);
            } else if (obj.event == 'unfreeze') {
                var form = new FormData();
                form.append('userid', obj.data.id);
                form.append('adminid', adminid);
                checkUser("/a/sys/Thaw", form);
            } else if (obj.event == 'orderPass') {
                var form = new FormData();
                form.append('orderid', obj.data.orderid);
                form.append('userid', obj.data.id);
                form.append('adminid', adminid);
                checkUser("/m/user/orderConsent", form);
            } else if (obj.event == 'orderRefuse') {
                var form = new FormData();
                form.append('orderid', obj.data.orderid);
                form.append('userid', obj.data.id);
                form.append('adminid', adminid);
                checkUser("/m/user/orderrefuse", form);
            } else if (obj.event == 'Logistical') {
                var form = new FormData();
                form.append('orderid', obj.data.orderid);
                //  layer.msg("订单不存在");
                var data = getData('/a/sys/getLogistics');
                console.log(data)

                if (data) {
                    var title = data.name;
                    var waybill = data.waybill;
                    layer.open({
                        type: 1,
                        title: title,
                        skin: 'layui-layer-rim', //加上边框
                        area: ['420px', '240px'], //宽高
                        content: "<div style='padding:30px 20px'>" + waybill + "</div>"
                    });
                } else {
                    layer.msg("暂无物流信息");
                }

                // checkUser("/a/sys/getLogistics", form);
            } else if (obj.event == 'orderSure') {

                layer.prompt({ title: '请输入快递公司', formType: 3 }, function(name, index) {
                    layer.close(index);
                    layer.prompt({ title: '请输入订单号', formType: 3 }, function(waybill, index) {
                        layer.close(index);
                        var form = new FormData();
                        form.append('orderid', obj.data.orderId);
                        form.append('waybill', waybill);
                        form.append('name', name);
                        form.append('adminid', adminid);
                        checkUser("/a/sys/IndentDeliver", form);
                    });
                });

            }
        });
    });








}